package com.llxjr.example.quartz.job.controller;

import com.llxjr.example.quartz.job.MyQuartzScheduler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 这里并没有采用restful风格 只是简单封装了一下api
 * 
 * @author yvan
 *
 */
@RestController
@RequestMapping("/quartz")
@Api("任务管理")
public class QuartzApiController {
    @Autowired
    private MyQuartzScheduler quartzScheduler;

    @ApiOperation("开始所有任务")
    @GetMapping("/start")
    public void startQuartzJob() {
        try {
            quartzScheduler.startJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("获取任务")
    @GetMapping("/getQuartzJob/{name}/{group}")
    public String getQuartzJob(@PathVariable String name, @PathVariable String group) {
        String info = null;
        try {
            info = quartzScheduler.getJobInfo(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return info;
    }


    @ApiOperation("修改任务")
    @GetMapping("/modify/{name}/{group}/{time}")
    public boolean modifyQuartzJob(@PathVariable String name, @PathVariable String group, @PathVariable String time) {
        boolean flag = true;
        try {
            flag = quartzScheduler.modifyJob(name, group, time);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @ApiOperation("暂停任务")
    @GetMapping("/pauseQuartzJob/{name}/{group}")
    public void pauseQuartzJob(@PathVariable String name, @PathVariable String group) {
        try {
            quartzScheduler.pauseJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("暂停所有任务")
    @GetMapping("/pauseAll")
    public void pauseAllQuartzJob() {
        try {
            quartzScheduler.pauseAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("删除所有任务")
    @DeleteMapping("/delete/{name}/{group}")
    public void deleteJob(@PathVariable String name, @PathVariable String group) {
        try {
            quartzScheduler.deleteJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    @ApiOperation("恢复某个任务")
    @GetMapping("/resumeJob/{name}/{group}")
    public void resumeJob(@PathVariable String name, @PathVariable String group) {
        try {
            quartzScheduler.resumeJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

}
